package rexsee.core.browser;

import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.widget.Div;
import android.app.Activity;

public class RexseeBrowserStyle implements JavascriptInterface {

	public static final String INTERFACE_NAME = "BrowserStyle";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return new RexseeBrowserStyle(childBrowser);
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeBrowserStyle(childBrowser);
	}

	private final Activity mActivity;
	private final Browser mBrowser;
	private final Div mDiv;
	private boolean syncTag = false;

	public RexseeBrowserStyle(Browser browser) {
		super();
		mBrowser = browser;
		mDiv = browser.containerDiv;
		mActivity = (Activity) browser.getContext();
	}

	public void setStyle(final String style) {
		syncTag = false;
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (mBrowser.parentBrowser == null) mDiv.setStyle(style);
				else mDiv.setStyleWithoutLayoutParams(style);
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(20);
			} catch (Exception e) {

			}
		}
	}
	public String getStyle(String name) {
		String value = mDiv.styleSheet.getStyle(name);
		return (value == null) ? "" : value;
	}
	public String getCurrentStyle(String name) {
		String value = mDiv.getStyle(name);
		return (value == null) ? "" : value;
	}

	public void startAnimation(final String style) {
		mDiv.animate(mBrowser, style);
	}
	public void stopAnimation() {
		mDiv.stopAnimate();
	}

}
